package com.xhs.zdl.demo.radioButton;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xhs.zdl.demo.MyUtils;
import com.xhs.zdl.demo.R;
import com.xhs.zdl.demo.adapter.NormalViewPagerAdapter;

import java.util.ArrayList;

public class HomePageDemo extends AppCompatActivity {

    private RadioGroup radio_group;
    private int[] icon_unchecked = {R.mipmap.home_page, R.mipmap.device, R.mipmap.message, R.mipmap.me};
    private int[] icon_checked = {R.mipmap.home_page_checked, R.mipmap.device_checked, R.mipmap.message_checked, R.mipmap.me_checked};
    private String[] icon_name = {"首页", "设备", "消息", "我"};
    private ArrayList<Fragment> fragment_list;
    private ViewPager viewPager;
    private NormalViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page_demo);

        findView();
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        //动态添加下方radiobutton到radiogroup
        RadioButton radioButton;
        Drawable drawable;
        for (int i = 0; i < icon_name.length; i++) {
            radioButton = new RadioButton(this);
            radioButton.setButtonDrawable(null);
            radioButton.setGravity(Gravity.CENTER);
            radioButton.setLayoutParams(new RadioGroup.LayoutParams(0, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
            //默认选中第一个
            if (i == 0){
                radioButton.setTextColor(Color.parseColor("#40B4F0"));
                drawable = getResources().getDrawable(icon_checked[i]);
            } else {
                radioButton.setTextColor(Color.parseColor("#666666"));
                drawable = getResources().getDrawable(icon_unchecked[i]);
            }
            radioButton.setText(icon_name[i]);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(null, drawable, null, null);
            radio_group.addView(radioButton);
        }

        //绑定监听器
        radio_group.setOnCheckedChangeListener(onCheckedChangeListener);

        //将Fragment实例化并加入list中
        fragment_list = new ArrayList<>();
        Fragment fragment;
        Bundle bundle;
        for (int i = 0; i < icon_name.length; i++) {
            fragment = new BlankFragment();
            bundle = new Bundle();
            bundle.putInt("index", i);
            fragment.setArguments(bundle);
            fragment_list.add(fragment);
        }

        //为viewPager设置适配器
        adapter = new NormalViewPagerAdapter(getSupportFragmentManager(), fragment_list);
        viewPager.setAdapter(adapter);
        //设置监听器
        viewPager.addOnPageChangeListener(onPageChangeListener);
    }

    /**
     * 拿到控件
     */
    private void findView() {
        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    //radiogroup的监听器
    RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            //遍历radioGroup
            for (int index = 0; index < radioGroup.getChildCount(); index++) {
                RadioButton rb = (RadioButton) radioGroup.getChildAt(index);
                if (rb.isChecked()) {
                    setCheckedRadioButton(index);
                    viewPager.setCurrentItem(index, false);
                } else
                    setUncheckedRadioButton(index);
            }
        }
    };

    //viewPager的监听器
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            MyUtils.Toast(HomePageDemo.this, "点击了第：" + (position+1) + "个fragment");
            for (int i = 0; i < radio_group.getChildCount(); i++) {
                if (i == position)
                    setCheckedRadioButton(i);
                else
                    setUncheckedRadioButton(i);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };

    /**
     * radioButton选中的样式设置
     * @param index     radioButton的下标
     */
    private void setCheckedRadioButton(int index){
        RadioButton rb = (RadioButton) radio_group.getChildAt(index);
        rb.setTextColor(Color.parseColor("#40B4F0"));
        Drawable drawable = getResources().getDrawable(icon_checked[index]);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        rb.setCompoundDrawables(null, drawable, null, null);
    }

    /**
     * radioButton未选中的样式设置
     * @param index     radioButton的下标
     */
    private void setUncheckedRadioButton(int index){
        RadioButton rb = (RadioButton) radio_group.getChildAt(index);
        rb.setTextColor(Color.parseColor("#666666"));
        Drawable drawable = getResources().getDrawable(icon_unchecked[index]);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        rb.setCompoundDrawables(null, drawable, null, null);
    }
}
